package com.kd.juhaowan.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;


import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import com.hjq.toast.ToastUtils;
import com.kd.juhaowan.R;
import com.kd.juhaowan.http.api.BindChangeOneApi;
import com.kd.juhaowan.http.api.PhoneCodeApi;
import com.kd.juhaowan.http.model.HttpData;
import com.kd.juhaowan.model.LoginUser;
import com.kd.juhaowan.utils.LogUtil;
import com.kd.juhaowan.utils.MsgCountDown;

import butterknife.BindView;
import butterknife.OnClick;

public class BindChange1Activity extends BaseActivity {
    @BindView(R.id.activity_title)
    TextView titleView;
    @BindView(R.id.bindchange_tip)
    TextView tipText;

    @BindView(R.id.pwdphone_code_get)
    TextView codeView;
    @BindView(R.id.pwdphone_code)
    EditText codeEdit;

    private String phoneNumber;

    @Override
    protected void reInitDate() {

    }

    @OnClick(R.id.pwdphone_code_get)
    public void getCode(View view){

        EasyHttp.post(this).api(new PhoneCodeApi(phoneNumber,"sms_original_phone_verification")).request(new HttpCallbackProxy<HttpData<LoginUser>>(this){
            @Override
            public void onHttpSuccess(HttpData<LoginUser> result) {
                super.onHttpSuccess(result);
                ToastUtils.show("获取验证码成功");
                MsgCountDown.setCurrentTime();
                updateSmsCount();
            }
        });
    }
    private void updateSmsCount() {
        if (isFinishing()||isDestroyed()) {
            return;
        }

        int value = (int) ((System.currentTimeMillis() - MsgCountDown.getLastTime()) / 1000);
        if (value > 59) {
            codeView.setEnabled(true);
            codeView.setText("重新发送");
//            loginCode.setTextColor(Color.parseColor("#ffffff"));
        } else {
//            reGetCode.setTextColor(Color.parseColor("#a39dce"));
//			reGetCode.setTextColor(ViewUtil.getColorRs(activity,"fjd_white"));
            codeView.setEnabled(false);
            codeView.setText((60 - value) + "s");
            codeView.postDelayed(new Runnable() {

                @Override
                public void run() {
                    updateSmsCount();
                }
            }, 1000);
        }
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        phoneNumber = getIntent().getStringExtra("phone");
        tipText.setText("验证码将发送至"+phoneNumber);
        titleView.setText("手机号改绑");
//        setContentView(R.layout.activity_account_login);
    }




    @OnClick(R.id.actvity_back)
    public void onBackClick(){
        finish();
    }

    @OnClick(R.id.login_confirm)
    public void onConfirmClick(){
        String codeString = codeEdit.getText().toString().trim();


     if (TextUtils.isEmpty(codeString)){
            ToastUtils.show("请输入验证码");
        }else {
            EasyHttp.post(this).api(new BindChangeOneApi(codeString)).request(new HttpCallbackProxy<HttpData<LoginUser>>(this){
                @Override
                public void onHttpSuccess(HttpData<LoginUser> result, boolean cache) {
                    super.onHttpSuccess(result, cache);
                    String token = result.getData().getToken();
                    LogUtil.debugLog(token+"token");
                    Intent in=new Intent(BindChange1Activity.this,BindChange2Activity.class);
                    in.putExtra("token",token);
                    startActivityForResult(in,10);
                }
            });
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==10&&resultCode==RESULT_OK){
            AccountManagerActivity.needRefresh=true;
            finish();
        }
    }

    @Override
    protected int getViewLayoutResourceId() {
        return R.layout.activity_bindchangeone;
    }




}
